<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="content">
      <!-- 头部功能 -->
      <template #header>
        <div>
          <el-input
            class="search-input"
            placeholder="请输入内容"
            size="medium"
            clearable
            v-model="query"
            @clear="reloadUserlistQuery"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="reloadUserlistQuery"
              ></el-button>
            </template>
          </el-input>
          <el-button class="add-btn" type="primary" size="medium"
            >添加商品</el-button
          >
        </div>
      </template>
      <!-- 正文内容 -->
      <el-table
        :data="goods"
        style="width: 100%"
        border
        size="small"
        v-loading="loading"
      >
        <template #empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column
          type="index"
          label="序号"
          width="50px"
        ></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          min-width="290px"
        ></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="120px">
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="200px"
          prop="add_time"
          :formatter="formatDate"
        >
        </el-table-column>
        <el-table-column label="操作" width="120px" #default="{ row }">
          <el-tooltip effect="dark" content="编辑商品" placement="bottom">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
            ></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除商品" placement="bottom">
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteGoods(row.goods_id)"
            ></el-button>
          </el-tooltip>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagesize"
        :total="total"
        :current-page="pagenum"
        @current-change="reloadUserlistPage"
        @size-change="reloadUserlistSize"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import filters from "@/filters";
export default {
  data() {
    return {
      loading: false,
      total: 0,
      pagesize: 10,
      pagenum: 1,
      query: "",
      goods: [],
    };
  },
  methods: {
    reloadUserlistPage(newPageNum) {
      // console.log(this.pagenum,newPageNum);
      this.pagenum = newPageNum;
      this.initGoodListPage();
    },
    reloadUserlistSize(newPageSize) {
      // console.log(this.pagesize,newPageSize);
      this.pagenum = 1; // 每页条数发生变化，表示分页数量也将发生变化，所以需要重置页面访问
      this.pagesize = newPageSize;
      this.initGoodListPage();
    },
    reloadUserlistQuery() {
      this.pagenum = 1;
      this.initGoodListPage();
    },
    async initGoodListPage() {
      try {
        this.loading = true;
        let result = await this.$api.getGoodsList({
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        });
        if (result == "error") return;
        console.log(result);
        this.total = result.total;
        this.pagenum = result.pagenum * 1;
        this.goods = result.goods;
      } catch (error) {
        console.log("商品列表加载错误:", error);
      } finally {
        this.loading = false;
      }
    },
    formatDate(row, column) {
      let date = row[column.property]*1000;
      // console.log(date, filters.formatDate(date, "yyyy-MM-dd HH:mm:ss"));
      return filters.formatDate(date, "yyyy-MM-dd HH:mm:ss");
    },
    async deleteGoods(good_id) {
      try {
        let result = await this.$api.deleteGoodsByGid(good_id);
        if(result=='error') return
        this.$message.success("商品删除成功");
        this.initGoodListPage();
      } catch (error) {
        console.log("商品删除错误:", error);
      }
    },
  },
  created() {
    this.initGoodListPage();
  },
};
</script>


<style lang="less" scoped>
.content,
.el-pagination {
  margin-top: 20px;
}
.el-card {
  .el-table {
    font-size: 14px;
  }
}
.search-input {
  width: 300px;
}
.add-btn {
  margin-left: 20px;
}
</style>